package typejump;

import typejump.engine.Animation;
import javax.imageio.*;
import java.awt.image.BufferedImage;

/**
 * Stores the Animation sequences which passed to Sprites. There are a number of pre-set 
 * Animations stored as static final variables.
 * @author g
 *
 */
public class AnimationManager {
	public static final Animation SWAG_DROP = loadAnimation(
			new String[] {"swagdrop1.png", "swagdrop2.png", "swagdrop3.png"}, 0.2, false);
	public static final Animation FLOOR_BRICK = getStaticAnimation("floor1.png");
	public static final Animation FLOOR_BRICK_SQ = getStaticAnimation("floor2.png");
	public static final Animation HEART = getStaticAnimation("heart.png");
	public static final Animation SPARKLE = loadAnimation( new String[] {"sparkle01.png", "sparkle02.png"}, 0.2, false);
	public static final Animation SPARKLE_HOVER = loadAnimation( new String[] {"sparkle_green.png", "sparkle_violet.png"}, 0.2, false);
	public static final Animation SPARKLE_SLOWFALL = loadAnimation( new String[] {"sparkle_violet.png"}, 0.2, false);
	public static final Animation LLAMOW_IDLE = loadAnimation( new String[] {"cow1.png"}, 1000, false);
	public static final Animation LLAMOW_SIT = loadAnimation( 
			new String[] {"llamow_sit0.png", "llamow_sit1.png", "llamow_sit2.png", "llamow_sit3.png"}, 1, false);
	public static final Animation LLAMOW_STAND = loadAnimation( 
			new String[] {"llamow_sit3.png", "llamow_sit2.png", "llamow_sit1.png", "llamow_sit0.png"}, 0.2, false);
	public static final Animation LLAMOW_SITTING = getStaticAnimation("llamow_sit3.png");
	
	public static final Animation SPARKLE_RAINBOW = loadAnimation(
			new String[] {
					"sparkle_red.png",
					"sparkle_orange.png",
					"sparkle_yellow.png",
					"sparkle_green.png",
					"sparkle_blue.png",
					"sparkle_indigo.png",
					"sparkle_violet.png",
			},
			0.5,
			false
		);
			
			
	/**
	 * Returns a new single-frame animation
	 * @param fname
	 * @return
	 */
	public static Animation getStaticAnimation(String fname) {
		return loadAnimation(new String[] {fname}, 1, false);
	}
			
			
	/**
	 * Creates a new animation from the given file names. The files are assumed to be
	 * in the resources directory. 
	 * @param files - a String[] of file names
	 * @param duration - How long it takes to go through all the frames (seconds)
	 * @param bounce - If true, the Animation will alternatingly play backwards and forwards
	 * when looping, rather than starting from the beginning every iteration.
	 * @return
	 */
	public static Animation loadAnimation(String[] files, double duration, boolean bounce) {
		BufferedImage[] imgs = new BufferedImage[files.length];
		for (int i = 0; i < files.length; i++) {
			try {
				imgs[i] = ImageIO.read(AnimationManager.class.getResource("/typejump/res/" + files[i]));
			} catch (Exception e) {
				System.err.println("Unable to load frame " + files[i]);
				return null;
			}
		}
		
		return new Animation(imgs, duration, bounce);
	}
}
